﻿[cmdletbinding(DefaultParameterSetName="none")]
param
(
    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [ValidateScript({(Test-Path $_ -PathType Leaf)})] 
    [String]
    $ApplicationManifestFilePath = "D:\Tools\MESF\Main\Powershell\Deployer\ApplicationConfiguration\Test.xml",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [ValidateScript({(Test-Path $_ -PathType Leaf)})] 
    [String]
    $ManagedActionsFilePath = "D:\Tools\MESF\Main\Powershell\Deployer\ActionsRepository\ManagedActions.xml",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $Environment = "DEV",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $ApplicationName = "mytool",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $Version = "1.0"

)

$verboseValue = ($VerbosePreference -eq "Inquire")
$DebugValue = ($DebugPreference -eq "Inquire")

Write-Warning "$VerbosePreference => Switch is : $verboseValue"
Write-Warning "$DebugPreference => Switch is : $DebugValue"

Import-Module Application.Management.psd1 -Force
Initialize-Environment_v2 -ApplicationManifestPath $ApplicationManifestFilePath -ManagedActionsPath $ManagedActionsFilePath

$dscInstallerFilePath = "D:\Tools\MESF\Main\Powershell\APM\Scripts\APM-DscConfigurationBuilder_1.0.ps1"
$dscresourceFile = [System.IO.File]::ReadAllText($dscInstallerFilePath)
Invoke-Expression $dscresourceFile


$deploymentTarget = $ApplicationManifest.DeploymentTargets | ? {($_.Environment -eq $Environment) -and ($_.ApplicationName -eq $ApplicationName) -and ($_.Version -eq $Version)}
if (($deploymentTarget -eq $null) -or ($deploymentTarget.count -gt 0)) {throw new-Object System.Exception "Missing deployment Target for Application '$ApplicationName' version '$Version' on environment '$Environment' in application manifets file '$ApplicationManifestFilePath'"}

Initialize-APMWorkspace -DeploymentTarget $deploymentTarget
foreach($server in $deploymentTarget.Servers.Values)
{
    $server.ConfigurationRepositories | Generate-ConfiguredFiles
}

$ConfigurationRepository = Get-ConfigurationRepository -UniqueKey $deploymentTarget.configurationRepository
Sync-SvnWorkspace -Path $ConfigurationRepository.GetLocalFullRootPath($deploymentTarget) -Comment "Save APM Worskpace for application $($DeploymentTarget.applicationName) and version $($DeploymentTarget.Version)" -UserName $ConfigurationRepository.UserName -Password $ConfigurationRepository.Password

$dscConfigurationOutputPath = Join-Path -Path "D:\Tools\MESF\Main\Powershell\Deployer\DeploymentManifest" -ChildPath $deploymentTarget.environment
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath $deploymentTarget.ApplicationName
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath $deploymentTarget.Version
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath "Servers"
APMServer -DeploymentTarget $deploymentTarget -OutputPath $dscConfigurationOutputPath





